#include"../include/lib_headers.h"
char* mount_vfs(char *vfs_path) {
	FILE *fp = fopen(vfs_path, "rb");
	int j = 0;
	
	/* Insufficient Arguments */
	if(strlen(vfs_path) == 0){
		sprintf(output_message,"%s %s","mountvfs_FAILURE",ERR_VFS_MOUNT_00);	
		return output_message;
	}
	
	/* VFS already mounted */
	if(h != NULL && l1 != NULL){
		sprintf(output_message,"%s %s","mountvfs_FAILURE",ERR_VFS_MOUNT_03);	
		return output_message;
	}
	
	/* Data File Not Found */
	if (fp == NULL) {
		sprintf(output_message,"%s %s","mountvfs_FAILURE",ERR_VFS_MOUNT_01);	
		return output_message;
	}
	
	
	/* cannot read from the data file*/
	h = (hd*) malloc(sizeof(hd));	
	if(fread(h, sizeof(hd), 1, fp)!=1){
		sprintf(output_message,"%s %s","mountvfs_FAILURE",ERR_VFS_MOUNT_02);	
		return output_message;
	}
	
	l1 = (label*) malloc(sizeof(label));
	l1->hd_pointer = h;
	l1->next=NULL;


	/* VFS already mounted */
	if(strcmp(h->VFS_Label,vfs_path) != 0){
		sprintf(output_message,"%s %s","mountvfs_FAILURE",ERR_VFS_MOUNT_03);	
		return output_message;
	}	
	/* cannot read from the data file*/
	array=(fd*)malloc(sizeof(fd)*(h->maxFds));
	if(fread(array, sizeof(fd) * (h->maxFds), 1, fp)!=1){
		sprintf(output_message,"%s %s","mountvfs_FAILURE",ERR_VFS_MOUNT_02);	
		return output_message;
	}
	/* cannot read from the data file*/
	freelist=(int*)malloc(sizeof(int)*h->maxFds);
	if(fread(freelist, sizeof(int) * h->maxFds, 1, fp)!=1){
		sprintf(output_message,"%s %s","mountvfs_FAILURE",ERR_VFS_MOUNT_02);	
		return output_message;
	}
	
	
	if(h->usedFds == 0){
		sprintf(output_message,"%s","mountvfs_SUCCESS");	
		fclose(fp);
		mount_bst();		
		mount_hashTable();
		return output_message;		
	}
	else{	
		

		for (j = 0; j < l1->hd_pointer->usedFds; j++) {
			//printf("j value:%d\n",j);
			recreate_dir(&array[j]);
//			printf("arr->fullPath = %s\n", array[j].fullPath);
		}
		
		mount_hashTable();		
		mount_bst();		
		
		sprintf(output_message,"%s","mountvfs_SUCCESS");	
		fclose(fp);		
		return output_message;
	}
	
	
}

